#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e

echo "Installing python3-pip..."
echo "-----------------------------------------------------"
sudo apt install -y python3-pip
echo "python3-pip installed successfully."

echo "Installing development packages and libraries..."
echo "-----------------------------------------------------"
sudo apt-get install -y python3-tk libfuse2 graphviz libgraphviz-dev
echo "Development packages and libraries installed successfully."

echo "Installing pygraphviz via pip..."
echo "-----------------------------------------------------"
sudo pip install pygraphviz -i https://pypi.tuna.tsinghua.edu.cn/simple
echo "pygraphviz installed successfully."

echo "Checking Python version... Your Python Version is:"
python3 --version

# Auto-detect hailo_dataflow_compiler version
HAILO_COMPILER_PATTERN="hailo_dataflow_compiler-([0-9]+\.[0-9]+\.[0-9]+)-.*\.whl"
HAILO_COMPILER_FILE=$(ls | grep -E "$HAILO_COMPILER_PATTERN" | head -n 1)
HAILO_COMPILER_VERSION=$(echo "$HAILO_COMPILER_FILE" | sed -E "s/$HAILO_COMPILER_PATTERN/\1/")

# Auto-detect hailort Python package version
HAILORT_PY_PATTERN="hailort-([0-9]+\.[0-9]+\.[0-9]+)-.*\.whl"
HAILORT_PY_FILE=$(ls | grep -E "$HAILORT_PY_PATTERN" | head -n 1)
HAILORT_PY_VERSION=$(echo "$HAILORT_PY_FILE" | sed -E "s/$HAILORT_PY_PATTERN/\1/")

# Auto-detect hailort Debian package version
HAILORT_DEB_PATTERN="hailort_([0-9]+\.[0-9]+\.[0-9]+)_amd64\.deb"
HAILORT_DEB_FILE=$(ls | grep -E "$HAILORT_DEB_PATTERN" | head -n 1)
HAILORT_DEB_VERSION=$(echo "$HAILORT_DEB_FILE" | sed -E "s/$HAILORT_DEB_PATTERN/\1/")

# Version consistency check
if [ "$HAILORT_PY_VERSION" != "$HAILORT_DEB_VERSION" ]; then
    echo "ERROR: hailort Python package version ($HAILORT_PY_VERSION) does not match Debian package version ($HAILORT_DEB_VERSION)!"
    exit 1
fi

# Install hailo_dataflow_compiler
echo "Installing hailo_dataflow_compiler $HAILO_COMPILER_VERSION..."
echo "-----------------------------------------------------"
pip3 install "$HAILO_COMPILER_FILE" -i https://pypi.tuna.tsinghua.edu.cn/simple
echo "hailo_dataflow_compiler installed successfully."

# Install hailort Python package
echo "Installing hailort Python package $HAILORT_PY_VERSION..."
echo "-----------------------------------------------------"
pip3 install "$HAILORT_PY_FILE" -i https://pypi.tuna.tsinghua.edu.cn/simple
echo "hailort Python package installed successfully."

# Install hailort Debian package
echo "Installing hailort Debian package $HAILORT_DEB_VERSION via dpkg..."
echo "-----------------------------------------------------"
sudo dpkg -i "$HAILORT_DEB_FILE"
echo "hailort Debian package installed successfully."

echo "Cloning hailo_model_zoo repository..."
echo "-----------------------------------------------------"
git clone https://gitee.com/li-bozha0/hailo_model_zoo.git
cd hailo_model_zoo
pip install -e .
echo "hailo_model_zoo package installed successfully."
cd ..

echo "Verifying hailo and hailomz installation versions..."
echo "-----------------------------------------------------"
hailo -h
echo "-----------------------------------------------------"
hailo --version
hailomz --version
echo "-----------------------------------------------------"
echo "Installation completed successfully!"